//
//  SFCDiffViewController.m
//  SVNForCocoa
//
//  Created by Jeremy Pereira on 23/12/2013.
//  Copyright (c) 2013 Jeremy Pereira. All rights reserved.
//

#import "SFCDiffViewController.h"
#import "SFCHunkViewController.h"
#import "SFCTextFileDiff.h"

/*!
 *    @brief Methods added to SFCDiffFileParser for use in outline views and
 *           table views.
 */
@interface SFCDiffFileParser (SFCOutlineView)

-(id) childAtIndex: (NSInteger) index;

-(id) brief;
-(id) change;
-(id) full;
-(id) left;
-(id) right;

-(NSInteger) childCount;

@end

@interface SFCDiffCollection (SFCOutlineView)

@end

@interface SFCTextFileDiff (SFCOutlineView)

@end

@interface SFCDiffHunk (SFCOutlineView)

@end

@interface SFCDiffLine (SFCOutlineView)

-(id) brief;
-(id) change;
-(id) full;
-(id) left;
-(id) right;

-(NSInteger) childCount;

@end

/*!
 */

@implementation SFCDiffViewController
{
    NSMutableArray* hunkViewControllers;
}

@synthesize view = _view;
@synthesize diffs = _diffs;

-(id) init
{
    self = [super init];
    if (self != nil)
    {
		hunkViewControllers = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void) setDiffs: (SFCDiffCollection*) diffs
{
    _diffs = diffs;
    [[self view] reloadData];
}

#pragma mark NSOutlineViewDataSource

-(id) outlineView: (NSOutlineView*) outlineView
            child: (NSInteger) index
           ofItem: (id) item
{
    if (item == nil)
    {
		item = [self diffs];
    }
	return [item childAtIndex: index];
}

-(NSInteger) outlineView: (NSOutlineView*) outlineView
  numberOfChildrenOfItem: (id) item
{
    if (item == nil)
    {
		item = [self diffs];
    }
    return [item childCount];
}

-(BOOL) outlineView: (NSOutlineView*) outlineView
   isItemExpandable: (id)item
{
    return [self outlineView: outlineView numberOfChildrenOfItem: item] > 0;
}

-(id) outlineView: (NSOutlineView*) outlineView
objectValueForTableColumn: (NSTableColumn*) tableColumn
           byItem: (id) item
{
	return [item valueForKey: [tableColumn identifier]];
}

@end


@implementation SFCDiffCollection (SFCOutlineView)

-(id) childAtIndex: (NSInteger) index
{
	return [self diffAtIndex: index];
}

-(NSInteger) childCount
{
    return [self diffCount];
}

-(id) brief
{
    return @"#####";
}

-(id) full
{
    return @"SHOULD NOT SEE THIS";
}


-(id) change
{
    return @" ";
}

-(id) left
{
    return @"";
}

-(id) right
{
    return @"";
}


@end

@implementation SFCTextFileDiff (SFCOutlineView)

-(id) childAtIndex: (NSInteger) index
{
	return [self hunkAtIndex: index];
}

-(NSInteger) childCount
{
    return [self hunkCount];
}

-(id) brief
{
    return [[self fileName] lastPathComponent];
}

-(id) change
{
    return @" ";
}

-(id) full
{
    return [self fileName];
}

-(id) left
{
    return @"";
}

-(id) right
{
    return @"";
}

@end

@implementation SFCDiffHunk (SFCOutlineView)

-(id) childAtIndex: (NSInteger) index
{
	return [self lineAtIndex: index];
}

-(NSInteger) childCount
{
    return [self lineCount];
}

-(id) brief
{
    return [NSString stringWithFormat:
            NSLocalizedString(@"Hunk %ld", @""),
            (long)[self sequenceNumber]];
}

-(id) full
{
    return [self header];
}


-(id) change
{
    return @" ";
}

-(id) left
{
    return @"";
}

-(id) right
{
    return @"";
}


@end

@implementation SFCDiffLine (SFCOutlineView)

-(NSInteger) childCount
{
    return 0;
}

-(id) brief
{
    return @" ";
}

-(id) full
{
    return[self text];
}


-(id) change
{
    unichar diffType = [self diffType];
    return [NSString stringWithCharacters: &diffType length: 1];
}


-(id) left
{
    NSString* ret = @"";
    NSInteger number = [self leftLineNumber];
    if (number != NSNotFound)
    {
		ret = [NSString stringWithFormat: @"%ld", number];
    }
    return ret;
}

-(id) right
{
    NSString* ret = @"";
    NSInteger number = [self rightLineNumber];
    if (number != NSNotFound)
    {
		ret = [NSString stringWithFormat: @"%ld", number];
    }
    return ret;
}

@end